﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using WaterKit.Base;
using WaterKit.TcpUdp;

namespace Auction_Client
{
    /// <summary>
    /// LoginWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 登录系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (App.client.IsConnected)
                {
                    //取得用户名
                    string uName = txtUsername.Text.ToString();
                    //取得密码
                    string uPwd = txtPassword.Password.ToString();

                    //登录信息验证，不能为空且没有"|"字符
                    if (uName.Length < 1 || uPwd.Length < 1 || uName.IndexOf("|") > 0 || uPwd.IndexOf("|") > 0)
                    {
                        MessageBox.Show("用户名或密码不能为空且不能含有'|'字符");
                    }
                    else
                    {
                        App.sendData(App.cmdType.c01, uName + "|" + uPwd);
                    }

                    //阻塞进程,等待返回结果
                    int i = 0;
                    while (i < 5 && (!App.islogin))
                    {
                        i++;
                        Thread.Sleep(App.timespan / 5);
                    }
                    if (App.islogin)
                    {
                        //登录成功,返回主程序
                        this.DialogResult = true;
                    }
                }
                else
                {
                    MessageBox.Show("尚未连接服务器,不可以登录!");
                }
            }
            catch (Exception ex)
            {
                Log4C.WriteErrLog("发送登录", ex);
            }
        }

        /// <summary>
        /// 窗体拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left) this.DragMove();
        }
        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExitApp_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
            App.Current.Shutdown();
        }
    }
}
